package com.guli.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.eduService.Listener.SubjectListener;
import com.guli.eduService.entity.EduSubject;
import com.guli.eduService.entity.vo.ExcelSubjectData;
import com.guli.eduService.entity.vo.OneSubjectInfo;
import com.guli.eduService.entity.vo.TwoSubjectInfo;
import com.guli.eduService.mapper.EduSubjectMapper;
import com.guli.eduService.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.serviceBase.Entity.GuliExcepiton;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author zxc
 * @since 2023-04-02
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    // service实现类中，对于addSubject方法进行具体实现
    @Override
    public void addSubject(MultipartFile file , EduSubjectServiceImpl eduSubjectService) {
        try {
            // 获取所传入文件的输入流
            InputStream inputStream = file.getInputStream();

            // 调用EasyExcel中方法，来逐行获取excel文件中内容;

            // 方法一 ：
//            ExcelReader excelReader = EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectListener(eduSubjectService))
//                    .build();
//            ReadSheet readSheet = EasyExcel.readSheet(0).build();
//            excelReader.read(readSheet);
//            excelReader.finish();

            // 方法二 ：
            EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new GuliExcepiton(20002 , "添加课程失败！！！");
        }
    }

    // service中，实现获取所有课程分类信息
    @Override
    public List<OneSubjectInfo> getAllSubjects() {
        // 1.获取所有一级标题的课程信息
        QueryWrapper<EduSubject> oneQueryWrapper = new QueryWrapper<>();
        // 即，一级标题课程的父id为 0;
        oneQueryWrapper.eq("parent_id" , 0);
        List<EduSubject> oneSubjects = baseMapper.selectList(oneQueryWrapper);

        // 2.获取所有二级标题的课程信息
        QueryWrapper<EduSubject> twoQueryWrapper = new QueryWrapper<>();
        // 即，二级标题课程的父id不为 0;
        twoQueryWrapper.ne("parent_id" , 0);
        List<EduSubject> twoSubjects = baseMapper.selectList(twoQueryWrapper);

        // 3.定义所要返回的结果集合
        List<OneSubjectInfo> result = new ArrayList<>();

        // 4.遍历所获取到的所有一级标题课程
        for (EduSubject oneSubject : oneSubjects) {
            OneSubjectInfo oneSubjectInfo = new OneSubjectInfo();
            // （1）即，将当前课程信息从EduSubject类型，转化为OneSubjectInfo类型;
//            oneSubjectInfo.setId(oneSubject.getId());
//            oneSubjectInfo.setTitle(oneSubject.getTitle());
            BeanUtils.copyProperties(oneSubject , oneSubjectInfo);

            // （2）结果集合result中，添加所有的一级标题课程信息;
            result.add(oneSubjectInfo);

            List<TwoSubjectInfo> list = new ArrayList<>();
            // （3）遍历所有二级标题课程信息
            for (EduSubject twoSubject : twoSubjects) {
                // （4）当前正在二级标题的课程信息的父id 为一级标题课程信息的id ：
                if(oneSubject.getId().equals(twoSubject.getParentId())){

                    // （5）将当前二级标题的课程信息 添加到一级标题课程的list集合中;
                    TwoSubjectInfo twoSubjectInfo = new TwoSubjectInfo();
                    BeanUtils.copyProperties(twoSubject , twoSubjectInfo);
                    list.add(twoSubjectInfo);
                }
            }

            oneSubjectInfo.setList(list);
        }

        return result;
    }

    // 具体实现，根据id获取指定课程分类信息
    @Override
    public OneSubjectInfo getSubjectById(String oneSubjectId) {
        // 1.根据id，来获取指定的课程分类信息
        EduSubject eduSubject = this.getById(oneSubjectId);
        if(eduSubject == null){
            throw new GuliExcepiton(20001 , "不存在该课程分类信息！！！");
        }
        // 2.获取所有二级标题的课程信息
        QueryWrapper<EduSubject> twoQueryWrapper = new QueryWrapper<>();
        // 即，二级标题课程的父id不为 0;
        twoQueryWrapper.ne("parent_id" , 0);
        List<EduSubject> twoSubjects = baseMapper.selectList(twoQueryWrapper);

        // 3.新建OneSubjectInfo类对象，将上述数据都封装到该对象中;
        OneSubjectInfo oneSubjectInfo = new OneSubjectInfo();
        BeanUtils.copyProperties(eduSubject , oneSubjectInfo);
        // 4.定义所要返回的结果集合
        List<TwoSubjectInfo> list = new ArrayList<>();
        // 5.遍历所有二级标题课程信息
        for (EduSubject twoSubject : twoSubjects) {
            // 当前正在二级标题的课程信息的父id 为一级标题课程信息的id ：
            if(oneSubjectId.equals(twoSubject.getParentId())){
                // 将当前二级标题的课程信息 添加到一级标题课程的list集合中;
                TwoSubjectInfo twoSubjectInfo = new TwoSubjectInfo();
                BeanUtils.copyProperties(twoSubject , twoSubjectInfo);
                list.add(twoSubjectInfo);
            }
        }
        // 6.设置当前oneSubjectInfo对象的children;
        oneSubjectInfo.setList(list);
        return oneSubjectInfo;
    }
}
